package com.bigtree.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigtree.dao.BookDao;
import com.bigtree.domain.Book;
import com.bigtree.service.BookService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

// 继承ServiceImpl,切忌加上泛型，第一个是mapper,第二个是实体类
// 实现
@Service
public class BookServiceImpl extends ServiceImpl<BookDao,Book> implements BookService {
    @Autowired
    BookDao bookDao;

    @Override
    public Book getById1(int id) {
        return bookDao.getById1(id);
    }


    public Page<Book> getPage(int currentPage, int pageSize) {
        //分页
        Page<Book> page =new Page<>(currentPage,pageSize);
        /*page.setCurrent(currentPage);
        page.setSize(pageSize);*/
        bookDao.selectPage(page,null);
        return page;
    }



    // 先模糊查询，然后在分页。那么返回到浏览器里面，肯定是页的形式返回
    @Override
    public Page<Book> getPageLike(int current, int size, Book book) {
        //模糊查询
        LambdaQueryWrapper<Book> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(book.getType()),Book::getType,book.getType());
        lqw.like(Strings.isNotEmpty(book.getName()),Book::getName,book.getName());
        lqw.like(Strings.isNotEmpty(book.getDescription()),Book::getDescription,book.getDescription());
        //如果得到的数据不为空，那么执行模糊查询，where book:getName like %book.getName%

        //分页
        Page<Book> page =new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        bookDao.selectPage(page,lqw);
        return page;
    }
}
